############################################################## 
## MOD Title:		Save posts as drafts (upgrade from 1.0.17c to 1.0.18)
## MOD Author:		asinshesq < N/A > (Alan) N/A
## MOD Description:	Allow users to save their posts as drafts so they can begin a post and finish it later.
##			Since it is a draft, others will not see it until it is done.
##
## MOD Version:		upgrade from 1.0.17c to 1.0.18
## 
## Installation Level:	Easy
## Installation Time:	5 Minutes ( 1 minute with easymod) 
## Files To Edit:
##			search.php
##			includes/page_header.php
##			language/lang_english/lang_main.php
##			templates/subSilver/index_body.tpl
##			templates/subSilver/search_results_posts.tpl
##			templates/subSilver/search_results_topics.tpl
##
## Included Files:	N/A
##
## License:		http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##
############################################################## 
## For security purposes, please check: http://www.phpbb.com/mods/
## for the latest version of this MOD. Although MODs are checked
## before being allowed in the MODs Database there is no guarantee
## that there are no security problems within the MOD. No support
## will be given for MODs not found within the MODs Database which
## can be found at http://www.phpbb.com/mods/
############################################################## 
## Author Notes:	This is an upgrade mod.  You must already have version 1.0.17c installed in order to use this mod.
##
############################################################## 
## MOD History:
##
##   2005-07-23	- Version 1.0.18
##		  adds new link on index page (and renames the old one) so that user can choose to see his drafts
##		  and posts (listed by post) or see his drafts and the topics to which he has posted;
##
##		  shows user his drafts even if the user has no real posts;
##		  
##		  adds number of drafts to total number of search matches shown at the top of the results page;
##
##		  adds additional columns in drafts table showing for each draft: the name of the forum in which
##		  the draft will be posted, the subject of the draft (or the topic title if no subject) and 
##		  the time the draft was written or edited
##
##   2005-03-04	- Version 1.0.17c
##		  made additional minor changes required for mod validation (no changes in functionality)
##
##   2005-02-19	- Version 1.0.17b
##		  made additional minor changes required for mod validation (no changes in functionality)
##
##   2005-01-28	- Version 1.0.17a
##		  made minor changes required for mod validation (no changes in functionality)
##
##   2005-01-10	- Version 1.0.17
##		  added draft prune functions (note that auto prune will only work if you enable pruning in the ACP configuration page);
##		  changed draft delete checkbox to draft delete button; added number of drafts and drafts per user to Forum stats in ACP;
##		  fixed a post count and pagination error in search.php; fixed an error where if the user clicked to 
##		  page 2 or higher in the draft and post list and then went back to page 1 the draft list would no longer be there;
##		  changed it so that a guest who is not logged in will not see the save as drafts button; and
##		  deleted a spurious reference to switch_not_save_a_draft_button that is no longer needed as a result of version 1.0.16 changes.
##
##   2004-11-15	- Version 1.0.16
##		  fixed the treatment of polls in drafts so that they do not get lost when the draft is submitted as a real post;
##		  corrected small error in prune.php that would result in pruning deleting drafts; and simplified posting_body.tpl so that
##		  it no longer uses a <!-- END switch_not_save_as_draft_button --> concept (so that privmsg.php will not get messed up for people with PCP mod); as a result, got rid of all changes to privmsg.php 
##
##   2004-10-04	- Version 1.0.15
##		  corrected small error in functions_admin.php that counted drafts as posts when it tries to sync a single topic.
##
##   2004-09-18	- Version 1.0.14
##		  corrected small error in search.php that allowed it to count a draft as a new post when you click the new posts link on the index page;
##		  also corrected problem in functions_admin.php that counted drafts as posts when it tries to sync a forum or topic (e.g. when you hit 'resync' in the forum management part of the ACP);
##		  and modified things so that a user who isn't generally authorized to delete his own posts can now delete his own drafts
##
##   2004-09-05	- Version 1.0.13
##		  no change in functionality...just changed method of inserting draft as a new post when you finally submit a draft (since method used in version 1.0.11 had some unnecessary steps in it); 
##
##   2004-09-02	- Version 1.0.12
##		  broke up large find and replace blocks in mod to smaller bits as needed to get validation...no change in the coding from version 1.1.11; 
##
##   2004-08-29	- Version 1.0.11
##		  changed mod so that it in effect creates a brand new post (with a new post_id and, if the post is a new topic, a brand new topic_id) when a draft finally gets submitted;
##		  the old draft post (and topic if the draft is a new topic) get deleted when the new one is created;
##		  since post_id now will always fall in same order as post_time, got rid of the changes that had been in prior drafts to places where sorts were done by post_id;
##		  as a result, the order of post_id of all final posts is now the same as the order of post_time of all final posts (not critical but nice);
##		  also fixed a minor error where a BEGIN_TRANSACTION statement did not have a corresponding END_TRANSACTION statement when the user saved the post as a draft (which
##		  could have slowed up the board a bit when a user saves a draft)
##
##   2004-08-22	- Version 1.0.10
##		  tweaked the mod so that post_id and topic_id get updated at the time the draft is finally submitted as real (so that there are never any 'sort' bugs)
##
##   2004-08-20	- Version 1.0.9
##		  corrected the fix in 1.0.8
##
##   2004-08-20	- Version 1.0.8
##		  fixed an error that prevented any regular users who are not moderators from deleting their own drafts
##
##   2004-08-06	- Version 1.0.7
##		  fixed aditional errors in viewtopic that could have caused wrong displays when user hits newest or previous or next in a topic where there are unsubmitted drafts
##
##   2004-08-05	- Version 1.0.6
##		  fixed an error in viewtopic that caused wrong pagination on occassion when there were umsubmitted drafts in a topic
##
##   2004-07-24	- Version 1.0.5
##		  fixed a number of minor bugs and cleaned up code
##
##   2004-07-23	- Version 1.0.4
##		  fixed privmsg.php so that submit and preview buttons show up on pm posts
##
##   2004-07-23	- Version 1.0.3
##		  fixed minor problem where reply notifications didn't go out and post didn't get marked as new when you finally submit draft as a live post
##
##   2004-07-23	- Version 1.0.2
##		  fixed minor problem where board stats didn't get updated after you finally submit a draft as a live post
##
##   2004-07-18	- Version 1.0.1
##		  first release
##
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################


#
#-----[ OPEN ]------------------------------------------
#
search.php

#
#-----[ FIND ]------------------------------------------------
#
$show_results = ($show_results == 'topics') ? 'topics' : 'posts';

#
#-----[ AFTER, ADD ]------------------------------------------------
#
// start mod save posts as drafts ... if user clicked to see his drafts and posts or topics, set search_id to egosearch and set show_results correctly
if( $search_id == 'egosearch_by_topic' || $search_id == 'egosearch_by_post' )
{
	( $search_id == 'egosearch_by_topic' ) ? $show_results = 'topics' : $show_results = 'posts';
	$search_id = 'egosearch';
}
// end mod save posts as drafts

#
#-----[ FIND ]------------------------------------------------
# note: I intentionally have no command after this FIND because we are looking for the second occurence of the code block
				$show_results = 'topics';
				$sort_by = 0;

#
#-----[ FIND ]------------------------------------------------
# note: make sure you find the second occurence of the code block
				$show_results = 'topics';
				$sort_by = 0;

#
#-----[ REPLACE WITH ]------------------------------------------------
#
				// start mod save posts as drafts (and end mod too)...deleted the line that set $show_results to 'topics' because $show_results is now set earlier on if this is an egosearch
				$sort_by = 0;

#
#-----[ FIND ]------------------------------------------
#
		{
			message_die(GENERAL_MESSAGE, $lang['No_search_match']);
		}

		//
		// Finish building query (for all combinations)
		// and run it ...

#
#-----[ REPLACE WITH ]------------------------------------------
#

		{
			// start mod save posts as drafts (and end mod too)...added the check that skips the no match message if there are drafts to be shown
			if(!draft_on) message_die(GENERAL_MESSAGE, $lang['No_search_match']);
		}

		//
		// Finish building query (for all combinations)
		// and run it ...

#
#-----[ FIND ]------------------------------------------
#
	if ( $search_results != '' )

#
#-----[ REPLACE WITH ]------------------------------------------
#
	// start mod save posts as drafts (and end mod too)...added $draft_on part of next line so that it will execute the next piece of code if we are supposed to show drafts even when there are no search matches
	if ( $search_results != '' || $draft_on )

#
#-----[ FIND ]------------------------------------------
#
		if ( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
		}

		$searchset = array();
		while( $row = $db->sql_fetchrow($result) )
		{
			$searchset[] = $row;
		}

		$db->sql_freeresult($result);

#
#-----[ REPLACE WITH ]------------------------------------------
#
		// start mod save posts as drafts ...skip the actual search query if this is a search that is supposed to show drafts and there are no regular search results
		if ( $search_results != '' )
		{
			if ( !$result = $db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
			}

			$searchset = array();
			while( $row = $db->sql_fetchrow($result) )
			{
				$searchset[] = $row;
			}

			$db->sql_freeresult($result);
		}
		// end mod save posts as drafts

#
#-----[ FIND ]------------------------------------------
#
// start mod save posts as drafts...show all drafts at the top of the search results page when a user searches for all of his or her posts (egosearch)...
if ( $draft_on && $start == 0 )
{
	$sql = "SELECT *
		FROM " . POSTS_TABLE . " p , " . TOPICS_TABLE . " t
		WHERE p.post_draft = 1
		AND p.poster_id = ".$userdata['user_id']."
		AND p.topic_id = t.topic_id
		ORDER BY p.post_time ";

	$result = $db->sql_query($sql);

	if( !$result )
	{
		message_die(GENERAL_ERROR, "Couldn't obtain draft posts from database", "", __LINE__, __FILE__, $sql);
	}

	$draft = $db->sql_fetchrowset($result);
	if ( count($draft) > 0 )
	{
	$template->assign_block_vars('switch_show_drafts', array());
	$template->assign_block_vars("draft", array());
		for($i = 0; $i < count($draft); $i++)
		{
		$row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2'];

		$template->assign_block_vars("draft.draft_topics", array(
			"ROW_CLASS" => $row_class,
			"DRAFT_POST" =>  $draft[$i]['topic_title'],
			"DRAFT_POST_LOC" => append_sid("posting.$phpEx?mode=draft&amp;" . POST_POST_URL . "=".$draft[$i]['post_id'].""))
		);
		}
	}
}
// end mod save posts as drafts

#
#-----[ REPLACE WITH ]------------------------------------------
#
		// start mod save posts as drafts...show all drafts at the top of the search results page when a user searches for all of his or her posts (egosearch)...
		if ( $draft_on && $start == 0 )
		{
			$sql = "SELECT p.post_id, p.post_time, pt.post_subject, t.topic_title, f.forum_name
				FROM " . POSTS_TABLE . " p , " . POSTS_TEXT_TABLE . " pt, " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f
				WHERE p.post_draft = 1
				AND pt.post_id = p.post_id
				AND p.poster_id = ".$userdata['user_id']."
				AND p.topic_id = t.topic_id
				AND f.forum_id = p.forum_id
				ORDER BY p.post_time ";

			$result = $db->sql_query($sql);

			if( !$result )
			{
				message_die(GENERAL_ERROR, "Couldn't obtain draft posts from database", "", __LINE__, __FILE__, $sql);
			}

			$draft = $db->sql_fetchrowset($result);
			$total_drafts = count($draft);
			
			// cut this all off if there are no regular search matches and no drafts
			if( $search_results == '' && $total_drafts == 0 ) message_die(GENERAL_MESSAGE, $lang['No_search_match']);
			
			if ( $total_drafts > 0 )
			{
			$template->assign_block_vars('switch_show_drafts', array());
			$template->assign_block_vars("draft", array());
				for($i = 0; $i < $total_drafts; $i++)
				{
				$row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2'];
				$draft_post_date = create_date($board_config['default_dateformat'], $draft[$i]['post_time'], $board_config['board_timezone']);

				$template->assign_block_vars("draft.draft_topics", array(
					"ROW_CLASS" => $row_class,
					"DRAFT_POST_TIME" =>  $draft_post_date,
					"DRAFT_POST_SUBJECT" =>  ( $draft[$i]['post_subject'] ) ? $draft[$i]['post_subject'] : $draft[$i]['topic_title'],
					"DRAFT_POST_TOPIC" =>  $draft[$i]['topic_title'],
					"DRAFT_POST_FORUM" =>  $draft[$i]['forum_name'],
					"DRAFT_POST_LOC" => append_sid("posting.$phpEx?mode=draft&amp;" . POST_POST_URL . "=".$draft[$i]['post_id'].""))
				);
				}
			}
		}
		// end mod save posts as drafts

#
#-----[ FIND ]------------------------------------------
#
		$l_search_matches = ( $total_match_count == 1 ) ? sprintf($lang['Found_search_match'], $total_match_count) : sprintf($lang['Found_search_matches'], $total_match_count);

		$template->assign_vars(array(
			'L_SEARCH_MATCHES' => $l_search_matches,
			'L_DRAFT_POSTS' => $lang['Draft_posts'],

#
#-----[ REPLACE WITH ]------------------------------------------
#
		// start mod save posts as drafts (and end mod too)...added drafts to the match count and added text for draft posts in the next two blocks of code
		$total_match_count = $total_match_count + $total_drafts;
		$l_search_matches = ( $total_match_count == 1 ) ? sprintf($lang['Found_search_match'], $total_match_count) : sprintf($lang['Found_search_matches'], $total_match_count);
		( $show_results == 'topics' ) ? $real_posts = $lang['Topics_you_have_posted_to'] : $real_posts = $lang['Your_posts'];
		
		$template->assign_vars(array(
			'L_SEARCH_MATCHES' => $l_search_matches,
			'L_DRAFT_POSTS' => $lang['Draft_posts'],
			'L_REAL_POSTS' => $real_posts,
			'L_DRAFT_POST_TIME' =>  $lang['Draft_post_time'],
			'L_DRAFT_POST_SUBJECT' =>  $lang['Draft_post_subject'],
			'L_DRAFT_POST_TOPIC' =>  $lang['Draft_post_topic'],
			'L_DRAFT_POST_FORUM' => $lang['Draft_post_forum'],

#
#-----[ OPEN ]------------------------------------------
#
includes/page_header.php

#
#-----[ FIND ]------------------------------------------
#
	'L_SEARCH_SELF' => $lang['Search_your_posts'],

#
#-----[ REPLACE WITH ]------------------------------------------
#
	'L_SEARCH_SELF_BY_TOPIC' => $lang['Search_your_drafts_and_topics'],
	'L_SEARCH_SELF_BY_POST' => $lang['Search_your_drafts_and_posts'],

#
#-----[ FIND ]------------------------------------------
#
	'U_SEARCH_SELF' => append_sid('search.'.$phpEx.'?search_id=egosearch'),

#
#-----[ REPLACE WITH ]------------------------------------------
#
	'U_SEARCH_SELF_BY_TOPIC' => append_sid('search.'.$phpEx.'?search_id=egosearch_by_topic'),
	'U_SEARCH_SELF_BY_POST' => append_sid('search.'.$phpEx.'?search_id=egosearch_by_post'),

#
#-----[ OPEN ]------------------------------------------
#
language/lang_english/lang_main.php

#
#-----[ FIND ]------------------------------------------
#
$lang['Search_your_posts'] = 'View your drafts and posts';

#
#-----[ REPLACE WITH ]------------------------------------------
#
$lang['Search_your_posts'] = 'View your posts';

#
#-----[ FIND ]------------------------------------------
#
$lang['Draft_posts'] = 'Your draft posts';

#
#-----[ REPLACE WITH ]------------------------------------------
#
$lang['Search_your_drafts_and_topics'] = 'View your drafts and topics you have posted to';
$lang['Search_your_drafts_and_posts'] = 'View your drafts and posts';
$lang['Draft_posts'] = 'Your drafts';
$lang['Topics_you_have_posted_to'] = 'Topics you have posted to';
$lang['Your_posts'] = 'Your posts';
$lang['Draft_post_time'] = 'Time of Draft';
$lang['Draft_post_subject'] = 'Draft Post';
$lang['Draft_post_topic'] = 'Topic of Draft';
$lang['Draft_post_forum'] = 'Forum';

#
#-----[ OPEN ]------------------------------------------------
#
templates/subSilver/index_body.tpl

#
#-----[ FIND ]------------------------------------------------
#
		<a href="{U_SEARCH_NEW}" class="gensmall">{L_SEARCH_NEW}</a><br /><a href="{U_SEARCH_SELF}" class="gensmall">{L_SEARCH_SELF}</a><br />

#
#-----[ REPLACE WITH ]-----------------------------------------
#
		<a href="{U_SEARCH_NEW}" class="gensmall">{L_SEARCH_NEW}</a><br /><a href="{U_SEARCH_SELF_BY_TOPIC}" class="gensmall">{L_SEARCH_SELF_BY_TOPIC}</a><br /><a href="{U_SEARCH_SELF_BY_POST}" class="gensmall">{L_SEARCH_SELF_BY_POST}</a><br />

#
#-----[ OPEN ]------------------------------------------------
#
templates/subSilver/search_results_posts.tpl

#
#-----[ FIND ]------------------------------------------------
#
<table border="0" cellpadding="3" cellspacing="1" width="100%" class="forumline" align="center">

#
#-----[ BEFORE, ADD ]-----------------------------------------
#
<!-- start mod save posts as drafts -->
<!-- BEGIN switch_show_drafts -->
<table width="100%" cellspacing="2" cellpadding="2" border="0" align="center">
   <tr>
 	<td colspan="4" align="center" height="15" nowrap="nowrap"><br /><br /><b>{L_DRAFT_POSTS}</b></td>
  </tr>
</table>
<table class="forumline" width="100%" cellspacing="1" cellpadding="3" border="0" align="center">
 <tr>
	<th class="thHead" height="15" nowrap="nowrap">{L_DRAFT_POST_FORUM}</th>
	<th class="thHead" height="15" nowrap="nowrap">{L_DRAFT_POST_TOPIC}</th>
	<th class="thHead" height="15" nowrap="nowrap">{L_DRAFT_POST_SUBJECT}</th>
	<th class="thHead" height="15" nowrap="nowrap">{L_DRAFT_POST_TIME}</th>
  </tr>
<!-- END switch_show_drafts -->
<!-- BEGIN draft -->
  <!-- BEGIN draft_topics -->
  <tr class="{draft.draft_topics.ROW_CLASS}" align="center">
  	<td class="row1"><span class="postdetails">{draft.draft_topics.DRAFT_POST_FORUM}</span></td>
  	<td class="row2"><span class="postdetails">{draft.draft_topics.DRAFT_POST_TOPIC}</span></td>
  	<td class="row1"<span class="gen"><a href="{draft.draft_topics.DRAFT_POST_LOC}">{draft.draft_topics.DRAFT_POST_SUBJECT}</a></span></td>
	<td class="row2"><span class="postdetails">{draft.draft_topics.DRAFT_POST_TIME}</span></td>
  </tr>
  <!-- END draft_topics -->
<!-- END draft -->
<!-- BEGIN switch_show_drafts -->
</table>
<!-- END switch_show_drafts -->
<table width="100%" cellspacing="2" cellpadding="2" border="0" align="center">
   <tr>
 	<td colspan="4" align="center" height="15" nowrap="nowrap"><br /><br /><b>{L_REAL_POSTS}</b></td>
  </tr>
</table>
<!-- end mod save posts as drafts -->

#
#-----[ OPEN ]------------------------------------------
#
templates/subSilver/search_results_topics.tpl

#
#-----[ FIND ]------------------------------------------
#
<!-- start mod save posts as drafts -->
<!-- BEGIN switch_show_drafts -->
<table class="forumline" width="100%" cellspacing="1" cellpadding="3" border="0" align="center">
  <tr>
	<th class="thHead" colspan="1" height="15" nowrap="nowrap">{L_DRAFT_POSTS}</th>
  </tr>
<!-- END switch_show_drafts -->
<!-- BEGIN draft -->
  <!-- BEGIN draft_topics -->
  <tr>
	<td class="{draft.draft_topics.ROW_CLASS}" align="center"><span class="gen"><a href="{draft.draft_topics.DRAFT_POST_LOC}">{draft.draft_topics.DRAFT_POST}</a></span></td>
  </tr>
  <!-- END draft_topics -->
<!-- END draft -->
<!-- BEGIN switch_show_drafts -->
</table>
<br /><br />
<!-- END switch_show_drafts -->
<!-- end mod save posts as drafts -->

#
#-----[ REPLACE WITH ]------------------------------------------
#
<!-- start mod save posts as drafts -->
<!-- BEGIN switch_show_drafts -->
<table width="100%" cellspacing="2" cellpadding="2" border="0" align="center">
   <tr>
 	<td colspan="4" align="center" height="15" nowrap="nowrap"><br /><br /><b>{L_DRAFT_POSTS}</b></td>
  </tr>
</table>
<table class="forumline" width="100%" cellspacing="1" cellpadding="3" border="0" align="center">
 <tr>
	<th class="thHead" height="15" nowrap="nowrap">{L_DRAFT_POST_FORUM}</th>
	<th class="thHead" height="15" nowrap="nowrap">{L_DRAFT_POST_TOPIC}</th>
	<th class="thHead" height="15" nowrap="nowrap">{L_DRAFT_POST_SUBJECT}</th>
	<th class="thHead" height="15" nowrap="nowrap">{L_DRAFT_POST_TIME}</th>
  </tr>
<!-- END switch_show_drafts -->
<!-- BEGIN draft -->
  <!-- BEGIN draft_topics -->
  <tr class="{draft.draft_topics.ROW_CLASS}" align="center">
  	<td class="row1"><span class="postdetails">{draft.draft_topics.DRAFT_POST_FORUM}</span></td>
  	<td class="row2"><span class="postdetails">{draft.draft_topics.DRAFT_POST_TOPIC}</span></td>
  	<td class="row1"<span class="gen"><a href="{draft.draft_topics.DRAFT_POST_LOC}">{draft.draft_topics.DRAFT_POST_SUBJECT}</a></span></td>
	<td class="row2"><span class="postdetails">{draft.draft_topics.DRAFT_POST_TIME}</span></td>
  </tr>
  <!-- END draft_topics -->
<!-- END draft -->
<!-- BEGIN switch_show_drafts -->
</table>
<!-- END switch_show_drafts -->
<table width="100%" cellspacing="2" cellpadding="2" border="0" align="center">
   <tr>
 	<td colspan="4" align="center" height="15" nowrap="nowrap"><br /><br /><b>{L_REAL_POSTS}</b></td>
  </tr>
</table>
<!-- end mod save posts as drafts -->

#
#-----[ SAVE/CLOSE ALL FILES ]--------------------------------
#
# EoM

